#include <stdio.h>
#include <stdlib.h>

typedef struct Queue
{
    int* head; // 队头
    int* tail; // 队尾
    int* base; // 队列空间起点
    int size; // 队列大小
    int num;  // 队列元素个数
}queue;

queue initQueue(int size)
{
    queue q;
    // 开辟队列空间
    q.base = (int*)malloc(size * sizeof(int));
    if(q.base == NULL)
    {
        printf("队列空间开辟失败...\n");
        return q;
    }
    q.head = q.base;
    q.tail = q.base;
    q.size = size;
    q.num = 0;
    return q;
}

void push(queue* q, int elem)
{
    if(q->num >= q->size)
    {
        printf("队满...\n");
        return ;
    }
    if((q->tail - q->base)/sizeof(int) == q->size)
    {
        q->tail = q->base;
    }
    q->base[q->tail - q->base] = elem;
    q->tail++;
    q->num++;
}

int get(queue* q)
{
    int result;
    if(q->num == 0)
    {
        printf("队空...\n");
        return -1;
    }
    if((q->head - q->base)/sizeof(int) == q->size)
    {
        q->head = q->base;
    }
    result = q->base[q->head - q->base];
    q->head++;
    q->num--;
    return result;
}

int main()
{
    queue q = initQueue(5);
    queue* qp = &q;
    push(qp, 1);
    push(qp, 3);
    push(qp, 5);
    push(qp, 2);
    // push(qp, 6);
    printf("%d\n", get(qp));
    printf("%d\n", get(qp));
    printf("%d\n", get(qp));
    push(qp, 4);
    push(qp, 10);
    printf("%d\n", get(qp));
    printf("%d\n", get(qp));
    printf("%d\n", get(qp));
    // get(qp);
    printf("\n");
    return 0;
}
